let router = require('../router/Router');
let ipUtil = require('../util/ipUtil');

let safeboxService = require('../service/safeboxService');
let smsService = require('../service/SmsService');

const { BusiError, SysError } = require('../exception/exception');


router.post("/auth/safebox/out", function (req, resp) {
    let amt = req.body.amt;

    amt = parseFloat(amt).toFixed(2);
    if (amt <= 0) {
        resp.send({code: 500, msg: "invalid amt"});
        return;
    }

    safeboxService.out({uid: req.uid, amt: amt, pwd: req.body.pwd}).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError){
            msg = e.message;
        }

        resp.send({code: 500, msg: msg});
    });
});

/**
 * 保险箱信息
 */
router.post("/auth/safebox", function (req, resp) {
    safeboxService.get(req.uid).then(res => {
        if (res) {
            resp.send({code: 200, msg: "success", data: res.amt});
        } else {
            resp.send({code: 500, msg: "查询失败"});
        }
    }).catch(e => {
        console.logw("err:", e);
        resp.send({code: 500, msg: e});
    });
});

router.post("/auth/safebox/valid/code", function (req, resp) {
    let ip = ipUtil.getIp(req, resp);
    smsService.getModifySafeBoxValidCode(ip, req.uid, req.body.mobile).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError){
            msg = e.message;
        }

        resp.send({code: 500, msg: msg});
    });
});

router.post("/auth/safebox/pwd/update/code", function (req, resp) {
    let ip = ipUtil.getIp(req, resp);

    safeboxService.updatePwdByCode(req.uid, req.body.mobile, req.body.pwd, req.body.code).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError){
            msg = e.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

router.post("/auth/safebox/setpwd", (req, resp) => {
    if (!req.body.pwd || req.body.pwd === '') {
        resp.send({code: 500, msg: "invalid pwd"});
        return;
    }

    safeboxService.new(req.uid, req.body.pwd).then(() => {
        resp.send({code: 200, msg: "设置成功"});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError){
            msg = e.message;
        }

        resp.send({code: 500, msg: msg});
    });
});

router.post("/auth/safebox/updatepwd", (req, resp) => {
    if (!req.body.pwd || req.body.pwd === '') {
        resp.send({code: 500, msg: "invalid pwd"});
        return;
    }

    if (!req.body.oldPwd || req.body.oldPwd === '') {
        resp.send({code: 500, msg: "invalid oldPwd"});
        return;
    }

    safeboxService.updatePwdWithPrePwd(req.uid, req.body.oldPwd, req.body.pwd).then(() => {
        resp.send({code: 200, msg: "设置成功"});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: "设置失败"});
    });
});

/**
 * 保险箱存入
 */
router.post("/auth/safebox/in", function (req, resp) {
    let amt = req.body.amt;

    amt = parseFloat(amt).toFixed(2);
    if (amt <= 0) {
        resp.send({code: 500, msg: "invalid amt"});
        return;
    }

    safeboxService.in({uid: req.uid, amt: amt, pwd: req.body.pwd}).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw("err:", e);
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError){
            msg = e.message;
        }

        resp.send({code: 500, msg: msg});
    });
});
